var loginid,loginuser,loginname,loginkey;
var isjump = 0;//控制翻页
layui.config({
	version:'1510164124181'
}).use(['form','laydate', 'laypage', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
    laydate = layui.laydate;//日期
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
	loginid = $.session.get("userid");
	loginuser = $.session.get("admin_realname");
	loginname = $.session.get("username");
	loginkey = $.session.get("user_secretkey");
	//加载默认数据
	//console.log("===="+window.sessionStorage.getItem("refreshdata_addrs"));
 	if(window.sessionStorage.getItem("refreshdata_addrs")){
 		var data = JSON.parse(window.sessionStorage.getItem("refreshdata_addrs"));
		pageNo = data.pageNo;
 	}
	myrefresh();
});
//查询用户
var initdata=[];
var refreshdata_addrs;
function inituserdata(pageRecords,pageNo){
	refreshdata_addrs='{"pageNo":"'+pageNo+'"}';
	window.sessionStorage.setItem("refreshdata_addrs",refreshdata_addrs);
	//console.log("['" + loginname + "','" + loginkey + "','"+pageRecords+"','"+pageNo+"']");
	$.ajax({
		url: "../../../../service",
		cache: false,
		async: false,
		data: {
			proc: "c_admin.community_selall",
			para: "['" + loginname + "','" + loginkey + "','"+pageRecords+"','"+pageNo+"']"
		},
		dataType: "json",
		success: function(data) {
			//console.log(JSON.stringify(data));
			if(data[0].TAG=='0'){
				initdata=[];
				top.layer.alert(""+data[0].MSG+"",function(index){
				   top.location.href="login.html";
				   layer.close(index);
			    });
			}else if(data[0].TAG=='2'){
			    initdata=[];
			}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
				initdata=[];
			}else{
			    initdata =data;
			}
			var dataHtml="";
			$(".data_content").html("");
			if(initdata.length==0){
				  totalRecords =0;
				  dataHtml += '<tr>'
					+'<td colspan="3" style="text-align:center;">未找到任何数据</td>'
					+'</tr>';
				page();
			}else{
                var caozuo = '';
				for(var i=0;i<initdata.length;i++){
					if(i==initdata.length-1){
						totalRecords = initdata[i].ID;
						totalPage = initdata[i].CNAME;
					}else{
						if(initdata[i].PAID==0){
							caozuo = '<a href="#" onclick="newchildcommuity(this)" data-id="'+initdata[i].ID+'">新增下级</a>&nbsp;&nbsp;&nbsp;<a href="#" onclick="edietaddr(this)" data-id="'+initdata[i].ID+'">编辑</a>&nbsp;&nbsp;&nbsp;<a href="#" onclick="userdelete(this)" data-id="'+initdata[i].ID+'">删除</a>';
						}else{
							caozuo = '<a href="#" onclick="edietaddr(this)" data-id="'+initdata[i].ID+'">编辑</a>&nbsp;&nbsp;&nbsp;<a href="#" onclick="userdelete(this)" data-id="'+initdata[i].ID+'">删除</a>';
						}
						dataHtml += '<tr>'
								+'<td align="center">'+initdata[i].ID+'</td>'
                                +'<td align="center">'+initdata[i].CNAME+'</td>'
						    	+'<td align="center">'+caozuo+'</td>'
								+'</tr>';
					}
				}
			}
			$(".data_content").html(dataHtml);
            //执行一个laypage实例,保证每个方法只进一次
            if(isjump==0){
				page();
			}
           isjump++;

		}

	});

}
//分页
function page(){
	laypage.render({
		elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
		,count:totalRecords //数据总数，从服务端得到
		,curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
        ,hash: 'fenye' //自定义hash值
		,layout: ['count', 'prev', 'page', 'next', 'skip']
		,jump: function(obj, first){
			//首次不执行
			if(!first){
			  //console.log("当前页==="+obj.curr); //得到当前页，以便向服务端请求对应页的数据。
			  //console.log("每页显示条数==="+obj.limit); //得到每页显示的条数
			  pageNo = obj.curr;

			  inituserdata(obj.limit,obj.curr);
			}
		  }
	});
}
//删除
function userdelete(obj){
	var uid = $(obj).attr("data-id");
	//console.log("['" + loginname + "','" + loginkey + "',"+(uid*1)+"]");
	top.layer.confirm('删除该地区也将会删除该地区下的所有地区，确认继续吗？',{title:'提示信息'},function(index){
		$.ajax({
			url: "../../../../service",
			cache: false,
			data: {
				proc: "c_admin.community_del",
				para: "['" + loginname + "','" + loginkey + "',"+(uid*1)+"]"
			},
			dataType: "json",
			success: function(data) {
				//console.log("====="+JSON.stringify(data));
				if(data[0].TAG=='0'){
					top.layer.closeAll();
					top.layer.alert(""+data[0].MSG+"",function(index){
					   top.location.href="login.html";
					   layer.close(index);
					});
				}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
					top.layer.closeAll();
					console.log("读取接口失败,参数错误");
				}else if(data[0].TAG =='2'){
					layer.msg(""+data[0].MSG+"");
				}else{
					top.layer.closeAll();
					layer.msg(""+data[0].MSG+"");
					isjump = 0;
                    inituserdata(pageRecords,pageNo);
				}
			}
		});
		layer.close(index);
	});
}
//跳转添加父级社区
function newcommuity(){
	$.session.set("newcommuity",0);
	$.session.set("edite_commid",0);
	window.location.href='add_area.html';
}
//编辑地区
function edietaddr(obj){
	var uid = $(obj).attr("data-id");
	$.session.set("newcommuity",1);
	$.session.set("edite_commid",uid);
	window.location.href='add_area.html';
}
//添加下级地区
function newchildcommuity(obj){
	var uid = $(obj).attr("data-id");
	$.session.set("newcommuity",0);
	$.session.set("edite_commid",uid);
	window.location.href='add_area.html';
}
//定时刷新数据
function myrefresh(){
  isjump=0;
  inituserdata(pageRecords,pageNo);
}
setInterval(myrefresh, 5000);
